package com.dykj.fireFighting.web.videoGroup;

import java.util.*;
import java.util.stream.Collectors;

import com.dykj.fireFighting.api.base.video.entity.Video;
import com.dykj.fireFighting.api.base.video.vo.VideoVo;
import com.dykj.fireFighting.api.socialUnit.videoGroup.VideoGroupDto;
import com.dykj.fireFighting.api.socialUnit.videoGroup.entity.VideoGroup;
import com.dykj.fireFighting.api.socialUnit.videoGroup.service.IVideoGroupService;
import com.dykj.fireFighting.api.socialUnit.videoGroup.vo.VideoGroupVo;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;



/**
 * 摄像头分组管理接口
 *
 * @Author JacksonTu
 * @Date  2022-02-28 14:25:03
 */
@Slf4j
@Api(value = "摄像头分组管理接口", tags = {"摄像头分组管理接口"})
@RestController
@RequestMapping("videoGroup")
public class VideoGroupController {

    @DubboReference
    private IVideoGroupService videoGroupService;

    /**
     * 摄像头分组管理列表
     *
     * @return
     */
    @AutoLog(value = "摄像头分组管理-分页列表查询")
    @ApiOperation(value = "摄像头分组管理分页列表", notes = "摄像头分组管理分页列表")
    @GetMapping("/list")
    public CommonResult<PageVo<VideoGroupVo>> treeGrid(VideoGroupDto videoGroupDto) {
        videoGroupDto.setUnitId(ShiroUtil.getUser().getEnterpriseId().toString());
        PageVo<VideoGroupVo> page=videoGroupService.queryList(videoGroupDto);
        return CommonResult.success(page);
    }

    /**
     * 获取摄像头分组管理信息
     */
    @AutoLog(value = "摄像头分组管理-获取摄像头分组管理信息")
    @ApiOperation(value = "获取摄像头分组管理信息", notes = "获取摄像头分组管理信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "Integer", required = true)
    @GetMapping("/info/{id}")
    public CommonResult<VideoGroup> info(@PathVariable("id") String id){
        VideoGroupVo videoGroup = videoGroupService.getInfoById(id);
        List<Video> videoList = videoGroup.getVideoList();
        videoGroup.setVideoIds(videoList.stream().map(Video::getId).collect(Collectors.toList()));
        return CommonResult.success(videoGroup);
    }

    /**
     * 保存摄像头分组管理信息
     */
    @AutoLog(value = "摄像头分组管理-保存摄像头分组管理信息")
    @ApiOperation(value = "保存摄像头分组管理信息", notes = "保存摄像头分组管理信息")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody VideoGroup videoGroup) {
        try {
            videoGroup.setId(UUIDUtils.get32UUID());
           videoGroup.setCreateBy(ShiroUtil.getUser().getName());
           videoGroup.setUnitId(ShiroUtil.getUser().getEnterpriseId().toString());
            videoGroup.setCreateTime(new Date());
                videoGroupService.insert(videoGroup);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 修改摄像头分组管理信息
     */
    @AutoLog(value = "摄像头分组管理-修改摄像头分组管理信息")
    @ApiOperation(value = "修改摄像头分组管理信息", notes = "修改摄像头分组管理信息")
    @PostMapping("/update")
    public CommonResult update(@Valid @RequestBody VideoGroup videoGroup) {
        try {

                videoGroupService.update(videoGroup);
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }

    /**
     * 删除摄像头分组管理信息
     */
    @AutoLog(value = "摄像头分组管理-删除摄像头分组管理信息")
    @ApiOperation(value = "删除摄像头分组管理信息", notes = "删除摄像头分组管理信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "Integer", required = true, allowMultiple = true)
    @PostMapping("/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
                videoGroupService.delete(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return CommonResult.failed("添加失败，请联系管理员");
        }
    }
    /**
     * 获取分组信息
     *
     * @return
     */
    @AutoLog(value = "获取分组信息")
    @ApiOperation(value = "获取分组信息列表", notes = "获取分组信息列表")
    @GetMapping("/groupList")
    public CommonResult groupList(VideoGroupDto videoGroupDto) {
        videoGroupDto.setUnitId(ShiroUtil.getUser().getEnterpriseId().toString());
      List<VideoGroup> list=videoGroupService.groupList(videoGroupDto);
        return CommonResult.success(list);
    }

    /**
     * 获取单位下所有分组监控设备
     *
     * @return
     */
    @AutoLog(value = "获取单位下某个分组监控设备")
    @ApiOperation(value = "获取单位下某个分组监控设备", notes = "获取单位下某个分组监控设备")
    @GetMapping("/getVideoByGroupUnit")
    public CommonResult getVideoByGroupUnit(VideoGroupDto videoGroupDto) {
        videoGroupDto.setUnitId(ShiroUtil.getUser().getEnterpriseId().toString());
        List<VideoVo> list=videoGroupService.getVideoByGroupUnit(videoGroupDto);
        return CommonResult.success(list);
    }
}
